exports.MMUser = Core.Class.subclass(
{
	classname:'MMUser',
								   
    initialize: function(userModel) 
    {
    	this._userModel = null;
    	this._allMalls = null;
    	this._allGifts = null;
    	
    	this._userModel = userModel;
    },
    
    loadUser: function()
    {
    	this._allMalls = this.initializeAllMalls();
    	this._allGifts = this.initializeAllGifts();
    },
    
    initializeAllMalls: function()
    {
    	var allMallModelsArray = new GAMutableArray();
    	var allMallsArray = this._userModel.allMalls;
    	if(allMallsArray) {
	    	var i = 0;
			for(var mallId in allMallsArray) {
				var mallModel = new MMMallModel(allMallsArray[mallId]);
				//mallModel._mallModel = allMallsArray[mallId];
				allMallModelsArray.insertObjectAtIndex(i, mallModel);
				i++;
			}
    	}
		
		return allMallModelsArray;
    },
    
    initializeAllGifts: function()
    {
    	var allGiftModelsArray = new GAMutableArray();
    	var allGiftsArray = this._userModel.allGifts;
    	if(allGiftsArray) {
	    	var i = 0;
			for(var giftId in allGiftsArray) {
				var giftModel = new MMGiftModel(allGiftsArray[giftId]);
				allGiftModelsArray.insertObjectAtIndex(i, giftModel);
				i++;
			}
    	}
		
		return allGiftModelsArray;
    },
    
    removeGift: function(gift)
    {
    	var id = gift.getGiftId();
		this._allGifts.removeObject(gift);
		delete this._userModel.allGifts[id];
		this.updateModel();
    },
    
    getAllMalls: function()
    {
    	return this._allMalls;
    },
    
    getAllGifts: function()
    {
    	return this._allGifts;
    },

    
    addMall: function(mallName)
    {
    	if(mallName) 
    	{
    		var mallModel = new MMMallModel();
    		mallModel._mallModel.mallId = this._userModel.mallId;
    		mallModel._mallModel.mallName = mallName;
    	
    		this._allMalls.addObject(mallModel);
    		
    		this._userModel.allMalls[mallModel._mallModel.mallId] = mallModel._mallModel;
    		this._userModel.mallId++;
    		
    		this.updateModel();
    		
    		return mallModel;
    	}
    	return null;	
    },
    
	addGift: function(giftJSON)
    {
    	if(giftJSON)
    	{
			var giftModel = new MMGiftModel();
			giftModel._giftObj.giftId = this._userModel.giftId;
			giftModel._giftObj.senderName = giftJSON.name;
			giftModel._giftObj.senderId = giftJSON.userid;
			giftModel._giftObj.itemId = giftJSON.itemid;
	
		
			this._allGifts.addObject(giftModel);
			
			this._userModel.allGifts[giftModel._giftObj.giftId] = giftModel._giftObj;
			this._userModel.giftId++;
			
			this.updateModel();
			
			return giftModel;
    	}	
    },
    
    updateModel: function()
	{
		if(this._userModel)
		{
			//this._userModel.persist();
			DatabaseInterface.updateUser();
		}
	},
    
    getCoins: function() {
    	return this._userModel.coins;
    },
    
    getNuggets: function() {
    	return this._userModel.nuggets;
    },
    
    updateCurrency: function (type, currency)
	{	
		if(currency <= 0) {
			currency = 0;
			return;
		}
		switch (type) {
			case kOperationType.kOperationAddCoins:
			{
				this._userModel.coins = this._userModel.coins + currency;
                break;
			}
			case kOperationType.kOperationSubtractCoins:
			{
				this._userModel.coins = this._userModel.coins - currency;
				break;
			}
			case kOperationType.kOperationAddNuggets:
			{	
				this._userModel.nuggets = this._userModel.nuggets + currency;
				break;
			}
			case kOperationType.kOperationSubtractNuggets:
			{
				this._userModel.nuggets = this._userModel.nuggets - currency;
				break;	
			}
			default:
				break;
		}
		
		if(this._userModel.coins > MM_MAX_USER_COINS) {
			this._userModel.coins = MM_MAX_USER_COINS;
		}
		if(this._userModel.coins <= 0) {
			this._userModel.coins = 0;
		}
		
		if(this._userModel.nuggets > MM_MAX_USER_NUGGETS) {
			this._userModel.nuggets = MM_MAX_USER_NUGGETS;
		}
		if(this._userModel.nuggets <= 0) {
			this._userModel.nuggets = 0;
		}
		
		this.updateModel();
		var controlLayer = MMGameLayer.getControlLayer();
		if(controlLayer)
		{
			var topBar = controlLayer.getTopBar();
			if(topBar)
			{
				MMGameLayer.getControlLayer().getTopBar().updateTopBar();
			}
		}
	},
	
	
	
    isValidPurchase: function(currencyType, defaultBuyPrice)
    {
    	var coins = MMGameState.getCurrentUser().getCoins();
    	var nuggets = MMGameState.getCurrentUser().getNuggets();
    	if (currencyType == kCurrencyType.kCurrencyTypeNuggets)
    	{
    		if (+defaultBuyPrice <= +nuggets)
    		{
    			return true;
    		}
    	}
    	else 
    	{
    		if(+defaultBuyPrice <= +coins)
    		{
    			return true;
    		}
    	}
    	return false;
    },	
    
    getFirstGameLaunchTime: function() 
    {
    	return this._userModel.firstGameLaunchTime;
    },

    setFirstGameLaunchTime: function(firstGameLaunchTime) 
    {
    	this._userModel.firstGameLaunchTime = firstGameLaunchTime;
    },
    
    isItemOpenForReward: function(item)
    {
		if(item.custom_attributes.unlock_time)
		{
			var firstLaunchTime = +this.getFirstGameLaunchTime();
			var unlockTime = +item.custom_attributes.unlock_time * SECS_IN_ONE_DAY;
			var currentTime = +MMGameState.getServerTime();
			var timePassed = currentTime - firstLaunchTime;
			if(unlockTime < timePassed)
			{	
				return true;
			}
			return false;
		}
		else 
		{
			return true;
		}
    },
    
    isUnlockedItemNew: function(item)
    {
    	var itemID = GACommonUtils.getKeyForStoreObject(item);
    	var unlockedItems = MMStorageManager.getValueFromUserDefault("unlockedItems");
    	
		var firstLaunchTime = +this.getFirstGameLaunchTime();
		var unlockTime = +item.custom_attributes.unlock_time * SECS_IN_ONE_DAY;
		var currentTime = +MMGameState.getServerTime();
		var timePassed = currentTime - firstLaunchTime;
    	
    	if(item.custom_attributes.unlock_time)
    	{
	    	if(unlockedItems)
	    	{
	    		var items = unlockedItems.split("|");
	    		if(items)
	    		{
		    		for(var i = 0 ; i < items.length ; i++)
		    		{
		    			if(items[i] == itemID)
		    			{
		    				return false;
		    			}
		    		}
					if(unlockTime < timePassed)
					{
						return true;
					}
					return false;
	    		}
	    	}
	    	else 
	    	{
				if(unlockTime < timePassed)
				{
					return true;
				}
				return false;
	    	}
    	}
    	return false;
    },
    
    saveUnlockedItemToUserDefaults: function(item)
	{
		var itemID = GACommonUtils.getKeyForStoreObject(item);
		var unlockedItems = MMStorageManager.getValueFromUserDefault("unlockedItems");
		if(unlockedItems == null)
		{
			unlockedItems = ""+itemID;
		}
		else
		{
			unlockedItems = unlockedItems +"|"+ itemID;	
		}
		MMStorageManager.saveToUserDefault("unlockedItems",unlockedItems);
	},
    
    checkForFirstLaunch: function()
    {
		if(this.getFirstGameLaunchTime() == 0)
		{
			this.setFirstGameLaunchTime(MMGameState.getServerTime());
			this.setGamerTag(GAConfigurations.getUserId());
			this.setHasCompletedTutorial(false);
			this.setCurrentTutorialStep(1);
			this.updateModel();
			return true;
		}
		return false
    },
    
    getCurrentTutorialStep: function()//battle heart
    {
    	return this._userModel.currentTutorialStep;
    },
	
    setCurrentTutorialStep: function(currentTutorialStep)
    {
    	this._userModel.currentTutorialStep = currentTutorialStep;
    },
    
    getHasCompletedTutorial: function()
    {
    	return this._userModel.hasCompletedTutorial;
    },
    
    setHasCompletedTutorial: function(tutorialStatus)
    {
    	this._userModel.hasCompletedTutorial = tutorialStatus;
    },
    
    getGamerTag: function()
    {
    	return this._userModel.gamerTag;
    },
    
    setGamerTag: function(gamerTag)
    {
    	this._userModel.gamerTag = gamerTag;
    },
    
    incrementXp: function()
    {
    	this._userModel.xp = +this._userModel.xp + 1
    	this.updateModel();
    },
   
	destroy: function()
	{
		this._userModel = null;	
		if(this._allMalls) 
		{
			this._allMalls.destroyWithObjects();
			this._allMalls = null;
		}
	},
});